//
//  ByMineSettingAddressManageVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByAddressManageVC.h"

// vc
#import "ByAddressAddNewVC.h"       // 新增地址

// cell
#import "ByCellAddressListItem.h"

// model
#import "ByModelAddress.h"

@interface ByAddressManageVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation ByAddressManageVC

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"收货地址";
    [self addLeftBackBarButtonItem];
    
    [self addBYItemviews:@[@"新建"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
    [self createTableView];
    
    [self getAddressList];
}

#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

#pragma mark - 请求数据
- (void)getAddressList {
    
    ByModelAddress *model1 = [ByModelAddress new];
    model1.isDefault = true;
    model1.name = @"朱凯";
    model1.mobile = @"12345678900";
    model1.address = @"浙江嘉兴南湖区浙江嘉兴南湖区浙fsdfsdfsfsdfsfsdfs江嘉兴南湖区浙江嘉兴南湖区浙江嘉兴南湖区浙江嘉兴南湖区";
    
    ByModelAddress *model2 = [ByModelAddress new];
    model2.isDefault = false;
    model2.name = @"朱佳样";
    model2.mobile = @"12345678900";
    model2.address = @"浙江嘉兴南湖区";
    
    [self.items removeAllObjects];
    [self.items addObject:model1];
    [self.items addObject:model2];
}

#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    [self.table registerClass:[ByCellAddressListItem class] forCellReuseIdentifier:@"addressCell"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.items.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellAddressListItem *cell = [tableView dequeueReusableCellWithIdentifier:@"addressCell"];
    cell.model = self.items[indexPath.section];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"addressCell" cacheByIndexPath:indexPath configuration:^(ByCellAddressListItem *cell) {
        cell.model = self.items[indexPath.section];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    if (self.didSelectedAddress) {
        self.didSelectedAddress(self.items[indexPath.section]);
        [self dismissViewControllerAnimated:true completion:nil];
    }
}

#pragma mark - 点击事件
- (void)rightItemsAction:(NSInteger)rightIndex {
    
    [self by_normalPush:[ByAddressAddNewVC new] hasNavi:true];
}


@end
